/**
 * @date 2006-7-23
 */

package org.nanhill.moo.service.impl;

import org.nanhill.moo.dao.FavorTagDao;
import org.nanhill.moo.dao.TagDao;
import org.nanhill.moo.dao.UserTagDao;
import org.nanhill.moo.domain.Favor;
import org.nanhill.moo.domain.FavorTag;
import org.nanhill.moo.domain.Tag;
import org.nanhill.moo.domain.User;
import org.nanhill.moo.domain.UserTag;
import org.nanhill.moo.service.TagService;

/**
 * @author alin [xalinx at gmail dot com]
 * @date 2006-7-23
 */
public class TagServiceImpl extends AbstractService implements TagService {

	private TagDao tagDao;

	public void setTagDao(TagDao tagDao) {
		this.tagDao = tagDao;
	}

	private UserTagDao userTagDao;

	public void setUserTagDao(UserTagDao userTagDao) {
		this.userTagDao = userTagDao;
	}

	private FavorTagDao favorTagDao;

	public void setFavorTagDao(FavorTagDao favorTagDao) {
		this.favorTagDao = favorTagDao;
	}

	public void openUserTags(final User owner, final User tagged,
			final Tag[] tags) {
		for (int i = 0; i < CONFIG.getUserTagMaxNumber() || i < tags.length; i++) {
			Tag thisTag = tags[i];
			Tag oldTag = this.tagDao.findByName(thisTag.getName());
			if (null == oldTag) {
				// this is new tag
				tagDao.save(thisTag);
				UserTag oldUserTag = userTagDao.findByUserIdTagId(tagged
						.getId(), thisTag.getId());
				if (null == oldUserTag) {
					// this is new UserTag
					UserTag newUserTag = new UserTag(tagged, thisTag);
					userTagDao.save(newUserTag);
					owner.addUserTag(newUserTag);
				} else {
					// this is old UserTag
					if (null == userTagDao.findByIdOwnerId(oldUserTag.getId(),
							owner.getId())) {
						// owner doesn't hold this old UserTag
						owner.addUserTag(oldUserTag);
					}
				}
			} else {
				// this is old Tag
				UserTag oldUserTag = userTagDao.findByUserIdTagId(tagged
						.getId(), oldTag.getId());
				if (null == oldUserTag) {
					// this is new UserTag
					UserTag newUserTag = new UserTag(tagged, oldTag);
					userTagDao.save(newUserTag);
					owner.addUserTag(newUserTag);
				} else {
					// this is old UserTag
					if (null == userTagDao.findByIdOwnerId(oldUserTag.getId(),
							owner.getId())) {
						// owner doesn't hold this old UserTag
						owner.addUserTag(oldUserTag);
					}
				}
			}
		}
	}

	public void openFavorTags(final User owner, final Favor tagged,
			final Tag[] tags) {
		for (int i = 0; i < CONFIG.getFavorTagMaxNumber() || i < tags.length; i++) {
			Tag thisTag = tags[i];
			Tag oldTag = this.tagDao.findByName(thisTag.getName());
			if (null == oldTag) {
				// this is new tag
				tagDao.save(thisTag);
				FavorTag oldFavorTag = favorTagDao.findByFavorIdTagId(tagged
						.getId(), thisTag.getId());
				if (null == oldFavorTag) {
					// this is new FavorTag
					FavorTag newFavorTag = new FavorTag(tagged, thisTag);
					favorTagDao.save(newFavorTag);
					owner.addFavorTag(newFavorTag);
				} else {
					// this is old FavorTag
					if (null == favorTagDao.findByIdOwnerId(
							oldFavorTag.getId(), owner.getId())) {
						// owner doesn't hold this old FavorTag
						owner.addFavorTag(oldFavorTag);
					}
				}
			} else {
				// this is old Tag
				FavorTag oldFavorTag = favorTagDao.findByFavorIdTagId(tagged
						.getId(), oldTag.getId());
				if (null == oldFavorTag) {
					// this is new FavorTag
					FavorTag newFavorTag = new FavorTag(tagged, oldTag);
					favorTagDao.save(newFavorTag);
					owner.addFavorTag(newFavorTag);
				} else {
					// this is old FavorTag
					if (null == favorTagDao.findByIdOwnerId(
							oldFavorTag.getId(), owner.getId())) {
						// owner doesn't hold this old FavorTag
						owner.addFavorTag(oldFavorTag);
					}
				}
			}
		}
	}

}
